﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using ClrExtensions.Net.Rest;
using MediaPortal.GUI.Library;

namespace MyNetflix2
{
    public class Search : oDataCalls
    {
        private User _user = null;
        
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="user"></param>
        public Search(User user)
            : base(user)
        { _user = user; }

        /// <summary>
        /// Search
        /// </summary>
        /// <param name="searchType"></param>
        /// <param name="discFormat"></param>
        /// <returns></returns>
        public new List<ODataItem> Refresh(string searchType, Formats.DiscFormat discFormat)
        {
            List<ODataItem> odataItems = new List<ODataItem>();
            DateTime backDate = DateTime.Now.Subtract(new TimeSpan(60, 0, 0, 0));
            DateTime forwardDate = DateTime.Now.AddDays(60);

            switch (searchType)
            {
                case "NewTelevision":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Genres('Television')/Titles?$filter=Instant/AvailableFrom gt datetime'" + backDate.Year.ToString() + "-" + (backDate.Month < 10 ? ("0" + backDate.Month.ToString()) : backDate.Month.ToString()) + "-" + (backDate.Day < 10 ? ("0" + backDate.Day.ToString()) : backDate.Day.ToString()) + "' and Instant/AvailableFrom le datetime'" + DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month < 10 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + "-" + (DateTime.Now.Day < 10 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + "'&$orderby=year(Instant/AvailableFrom)&$top=100", discFormat);
                    odataItems = SortByTitleOD(odataItems);
                    break;
                case "NewInstant":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Titles?$filter=Instant/AvailableFrom gt datetime'" + backDate.Year.ToString() + "-" + (backDate.Month < 10 ? ("0" + backDate.Month.ToString()) : backDate.Month.ToString()) + "-" + (backDate.Day < 10 ? ("0" + backDate.Day.ToString()) : backDate.Day.ToString()) + "' and Instant/AvailableFrom le datetime'" + DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month < 10 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + "-" + (DateTime.Now.Day < 10 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + "'&$orderby=year(Instant/AvailableFrom) desc&$top=100", discFormat);
                    break;
                case "NewDVD":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Titles?$filter=Dvd/AvailableFrom gt datetime'" + backDate.Year.ToString() + "-" + (backDate.Month < 10 ? ("0" + backDate.Month.ToString()) : backDate.Month.ToString()) + "-" + (backDate.Day < 10 ? ("0" + backDate.Day.ToString()) : backDate.Day.ToString()) + "' and Dvd/AvailableFrom le datetime'" + DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month < 10 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + "-" + (DateTime.Now.Day < 10 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + "'&$orderby=year(Dvd/AvailableFrom) desc&$top=100", discFormat);
                    break;
                case "NewBluRay":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Titles?$filter=BluRay/AvailableFrom gt datetime'" + backDate.Year.ToString() + "-" + (backDate.Month < 10 ? ("0" + backDate.Month.ToString()) : backDate.Month.ToString()) + "-" + (backDate.Day < 10 ? ("0" + backDate.Day.ToString()) : backDate.Day.ToString()) + "' and BluRay/AvailableFrom le datetime'" + DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month < 10 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + "-" + (DateTime.Now.Day < 10 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + "'&$orderby=year(BluRay/AvailableFrom) desc&$top=100", discFormat);
                    break;
                case "UpcomingTelevision":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Genres('Television')/Titles?$filter=Instant/AvailableFrom gt datetime'" + DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month < 10 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + "-" + (DateTime.Now.Day < 10 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + "' and Instant/AvailableFrom le datetime'" + forwardDate.Year.ToString() + "-" + (forwardDate.Month < 10 ? ("0" + forwardDate.Month.ToString()) : forwardDate.Month.ToString()) + "-" + (forwardDate.Day < 10 ? ("0" + forwardDate.Day.ToString()) : forwardDate.Day.ToString()) + "'&$orderby=year(Instant/AvailableFrom)&$top=100", discFormat);
                    odataItems = SortByTitleOD(odataItems);
                    break;
                case "UpcomingInstant":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Titles?$filter=Instant/AvailableFrom gt datetime'" + DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month < 10 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + "-" + (DateTime.Now.Day < 10 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + "' and Instant/AvailableFrom le datetime'" + forwardDate.Year.ToString() + "-" + (forwardDate.Month < 10 ? ("0" + forwardDate.Month.ToString()) : forwardDate.Month.ToString()) + "-" + (forwardDate.Day < 10 ? ("0" + forwardDate.Day.ToString()) : forwardDate.Day.ToString()) + "'&$orderby=year(Instant/AvailableFrom) desc&$top=100", discFormat);
                    break;
                case "UpcomingDVD":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Titles?$filter=Dvd/AvailableFrom gt datetime'" + DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month < 10 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + "-" + (DateTime.Now.Day < 10 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + "' and Dvd/AvailableFrom le datetime'" + forwardDate.Year.ToString() + "-" + (forwardDate.Month < 10 ? ("0" + forwardDate.Month.ToString()) : forwardDate.Month.ToString()) + "-" + (forwardDate.Day < 10 ? ("0" + forwardDate.Day.ToString()) : forwardDate.Day.ToString()) + "'&$orderby=year(Dvd/AvailableFrom) desc&$top=100", discFormat);
                    break;
                case "UpcomingBluRay":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Titles?$filter=BluRay/AvailableFrom gt datetime'" + DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month < 10 ? "0" + DateTime.Now.Month.ToString() : DateTime.Now.Month.ToString()) + "-" + (DateTime.Now.Day < 10 ? "0" + DateTime.Now.Day.ToString() : DateTime.Now.Day.ToString()) + "' and BluRay/AvailableFrom le datetime'" + forwardDate.Year.ToString() + "-" + (forwardDate.Month < 10 ? ("0" + forwardDate.Month.ToString()) : forwardDate.Month.ToString()) + "-" + (forwardDate.Day < 10 ? ("0" + forwardDate.Day.ToString()) : forwardDate.Day.ToString()) + "'&$orderby=year(BluRay/AvailableFrom) desc&$top=100", discFormat);
                    break;
            }

            return odataItems;
        }

        /// <summary>
        /// Search for Instant Titles
        /// </summary>
        /// <param name="searchType"></param>
        /// <param name="genre"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public new List<ODataItem> Refresh(string searchType, string genre, string title)
        {
            List<ODataItem> odataItems = new List<ODataItem>();

            switch (searchType)
            {
                case "InstantTitleSearch":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Titles?$filter=substringof('" + title + "', Name) and Instant/Available eq true&$top=100", Formats.DiscFormat.Instant);
                    break;
                case "InstantTitleTVSeriesSearch":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Titles?$filter=substringof('" + title + "', Name) and Instant/Available eq true and ReleaseYear gt 0&$top=100", Formats.DiscFormat.Instant);
                    break;
                case "InstantGenreSearch":
                    odataItems = base.Refresh("http://odata.netflix.com/Catalog/Genres('" + genre + "')/Titles?$filter=Instant/Available eq true&$top=100&$orderby=year(Instant/AvailableFrom) desc", Formats.DiscFormat.Instant);
                    break;
            }

            return odataItems;

        }

        /// <summary>
        /// NetFlix API Search
        /// </summary>
        /// <param name="resource"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public override List<QueueItem> Refresh(string term)
        {

            //--------------------------------------------------------------------------------
            // Set up the Rest call to get the Instant Queue
            //--------------------------------------------------------------------------------
            RestCall request = RestCalls.GetCatalogTitles(term, StartIndex, EndIndex);

            try
            {
                //--------------------------------------------------------------------------------
                // Get the Instant Queue from Netflix
                //--------------------------------------------------------------------------------
                string xml = base.NetFlixUser.NetflixConnection.RequestResource(request, base.NetFlixUser.Token);

                //--------------------------------------------------------------------------------
                // Parse to Linq
                //--------------------------------------------------------------------------------
                XElement root = XElement.Parse(xml);

                //--------------------------------------------------------------------------------
                // Add the elements to the QueueItems class
                //--------------------------------------------------------------------------------
                foreach (XElement node in root.Elements("catalog_title"))
                {
                    QueueItems.Add(new QueueItem(node, _user));
                }

                return QueueItems;
            }
            catch (Exception ex)
            {
                Log.Error("My NetFlix - InstantQueue:Refresh(): Error - " + ex.Message);
                return null;
            }
        }
    }
}
